/******************************************************************************
Copyright(c) 2016 - 2018 Digital Power Inc.
File name: WrapList.h
Author: liaosenlin
Version: 1.0.0
Date: 2018/6/7
Description: list相关的函数定义
History:
Bug report: liaosenlin@d-power.com.cn
******************************************************************************/
#ifndef __LIST_H__
#define __LIST_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdbool.h>

typedef struct list_head 
{
	struct list_head *next;
	struct list_head *prev;
}ls_list_head;

#ifndef offsetof
#define offsetof(type, member) \
((size_t)(&((type*)0)->member))
#endif

#ifndef container_of
#define container_of(ptr, type, member)					\
	({								\
		const __typeof__(((type *) NULL)->member) *__mptr = (ptr);	\
		(type *) ((char *) __mptr - offsetof(type, member));	\
	})
#endif
#define INIT_LIST_HEAD                  list_init
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#undef LIST_HEAD
#define LIST_HEAD(name)	struct list_head name = LIST_HEAD_INIT(name)

#define	list_entry(ptr, type, field)	container_of(ptr, type, field)

#define	list_first_entry(ptr, type, field)	list_entry((ptr)->next, type, field)

#define	list_last_entry(ptr, type, field)	list_entry((ptr)->prev, type, field)

#define	list_for_each(p, head)						\
	for (p = (head)->next; p != (head); p = p->next)

#define	list_for_each_safe(p, n, head)					\
	for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)

#define list_for_each_entry(p, h, field)				\
	for (p = list_first_entry(h, __typeof__(*p), field); &p->field != (h); \
	    p = list_entry(p->field.next, __typeof__(*p), field))


#define list_for_each_entry_safe(p, n, h, field)			\
	for (p = list_first_entry(h, __typeof__(*p), field),		\
	    n = list_entry(p->field.next, __typeof__(*p), field); &p->field != (h);\
	    p = n, n = list_entry(n->field.next, __typeof__(*n), field))

#define	list_for_each_entry_reverse(p, h, field)			\
	for (p = list_last_entry(h, __typeof__(*p), field); &p->field != (h); \
	    p = list_entry(p->field.prev, __typeof__(*p), field))


#define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)


#define	list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

static inline void __list_del_entry(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = NULL;
    entry->prev = NULL;
}

static inline void __list_add(struct list_head *_new,
                              struct list_head *prev,
                              struct list_head *next)
{
    next->prev = _new;
    _new->next = next;
    _new->prev = prev;
    prev->next = _new;
}

static inline void list_add(struct list_head *_new, struct list_head *head)
{
    __list_add(_new, head, head->next);
}

static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{
    __list_add(_new, head->prev, head);
}

static inline int list_is_last(const struct list_head *list,
                               const struct list_head *head)
{
    return list->next == head;
}

static inline int list_is_first(const struct list_head *list,
                                const struct list_head *head)
{
    return list->prev == head;
}

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

static inline int list_empty_careful(const struct list_head *head)
{
    struct list_head *next = head->next;
    return (next == head) && (next == head->prev);
}


static inline void list_init( struct list_head *list)
{
    list->next  = list;
	list->prev = list;
	
}



#ifdef __cplusplus
}
#endif

#endif	// 